﻿/*
pilot_getin.sqf
*/
/*
@author clint@team-r3f.org

Le script principal gérant tout le lift
appelé par EH quand le joueur monte ds l'helico
*/

//--------------Initialisation -------------------------
_vehicule	= _this select 0;
_role		= _this select 1;
_unit		= _this select 2;
_type_unite	= typeof _unit;
_type_de_vehicule	= typeof _vehicule;

if(  _role == "driver" ) then {
	Lift_Player_Is_Driver = true;
};

In_lift = false;
Vehicule_Relached = false;
Vehicule_Attached = objNull;
_menu_liftage_visible = false;
_objet_proche = objNull;
_id_menu = -1;
_id_menu_relache = -1;
_bonne_distance = false;

while {(alive _vehicule) && (alive player) && Lift_Player_Is_Driver} do {
	_pos = getPos _vehicule;
	_hauteur = _pos select 2;
	if (!In_Lift && ( _hauteur > Lift_Altitude_Min) && (_hauteur < Lift_Altitude_Max)) then {
		_objet_proche = objNull;
		_list_objets = nearestObjects [_vehicule, ["LandVehicle"], Lift_Approche_Max];
		if (count _list_objets > 0) then {
			_test = _list_objets select 0;
			if (_test == _vehicule) then {
				if (count _list_objets > 1) then {
					_objet_proche = _list_objets select 1;
				};
			} else {
				_objet_proche = _test;
			};
		};
		sleep 0.1;
		if (!(isNull _objet_proche) && _objet_proche != Vehicule_Attached && ((typeof _objet_proche) in Lift_Authorized_Type)) then {
			
			_objet_proche_pos = getPos _objet_proche;
			
			_X_objet = _objet_proche_pos select 0;
			_X_vehicule = _pos select 0;
			
			_Y_objet = _objet_proche_pos select 1;
			_Y_vehicule = _pos select 1;
			
			_distance = abs ((_X_objet + _Y_objet) - (_X_vehicule + _Y_vehicule));
			
			_bonne_distance = ( _distance <= Lift_Distance_Max );
			
			if ( _bonne_distance ) then {
				if (!_menu_liftage_visible) then {
					_id_menu = _vehicule addAction [localize "STR_ACTION_LIFT", "base\helilift\do_lift.sqf",-1,100000];
					_menu_liftage_visible = true;
				};
			} else {
				_objet_proche = objNull;
				if (_menu_liftage_visible) then {
					_vehicule removeAction _id_menu;
					_id_menu = -1;
					_menu_liftage_visible = false;
				};
			};
		};
	}else{
		if (_menu_liftage_visible) then {
			_vehicule removeAction _id_menu;
			_id_menu = -1;
			_menu_liftage_visible = false;
		};
		sleep 0.1;	
		if (isNull _objet_proche) then {
			In_Lift = false;
			Vehicule_Relached = false;
		} else {
			if (In_Lift) then {
				_id_menu_relache = _vehicule addAction [localize "STR_ACTION_UNLIFT", "base\helilift\undo_lift.sqf",-1,100000];
				Vehicule_Attached = _objet_proche;
				_objet_proche engineOn false;
				_objet_proche attachTo [_vehicule, Lift_Vehicule_Pos_Attache];
				_vehicle_name = getText(configFile>>"cfgVehicles" >> typeof Vehicule_Attached >> "displayName");
				[ format[localize "STR_ACTION_MSG_LIFT",_vehicle_name , name player]] call FNCT_RadioToAll;
				
				while {alive _vehicule && Lift_Player_Is_Driver && alive _objet_proche && alive player && !Vehicule_Relached} do {
					sleep 0.4;
				};
				
				detach _objet_proche;					
				_objet_proche setVelocity [0,0,0];
				_objet_proche engineOn false;

				[ format[localize "STR_ACTION_MSG_UNLIFT",_vehicle_name , name player]] call FNCT_RadioToAll;
					
				In_Lift = false;
				Vehicule_Relached = false;
				
				Vehicule_Attached = objNull;				
				if (!alive _vehicule) then {
					_vehicule removeAction _id_menu_relache;
				};
				_pos_lifted = position _objet_proche;
				_objet_proche setPos [_pos_lifted select 0, _pos_lifted select 1, 0];
				_objet_proche setVelocity [0,0,0];
				sleep 1;
			};
		};
	};
};

if (true) exitWith {};